<template>
  <view class="page-container">
    <view class="main-container">
      <view class="form-container">
        <view class="item">
          <textarea class="text-body" style="padding-left: 10px; padding-right: 10px;" v-model="message" name="" id="" cols="30" rows="10" minlength="10" maxlength="1000"></textarea>
          <view class="ctrls">
            <view class="camera" @click="uploadImg">
              <img src="~@/static/other_slices/active_camera_icon.png" alt="">
            </view>
            <view class="tip">最多输入 1,000 字</view>
          </view>
        </view>
        <view v-if="uploadPreview" class="item">
          <img :src="uploadPreview" alt="" style="width: 100%;">
        </view>
        <view class="item">
          <button type="button" @click="submit" class="btn-res">提交</button>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import Vue from 'vue'
import { token } from "@/util/storage";

function uuid() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = "-";

  var uuid = s.join("");
  return uuid;
}

export default Vue.extend({
  data() {
    return {
      message: '',
      uploadImgObj: null,
      uploadImgSrc: "",
      uploadPreview: "",
    }
  },
  onLoad(option) {
    console.log("option2", option)
    if (option) {
      if (!!option.jump) {

      } else {
        location.href = '/#/pages/login/wechat?redirect=' + encodeURI('/pages/other/suggestions?jump=1');
      }
    }
  },
  methods: {
    uploadImg() {
      uni.chooseImage({
        count: 1,
        sourceType: ['camera'],
        success: (res) => {
          console.log(res)
          this.uploadImgObj = res.tempFiles[0]
          this.uploadPreview = res.tempFilePaths[0]
        }
      });
    },
    uploadQiNiuFile(file, ) {
      return new Promise((resolve, reject) => {
        let fileName = file.name
        let fileExt = fileName.substr(fileName.lastIndexOf(".") + 1)
        let qiNiuKey = `app/${uuid()}.${fileExt}`
        this.$mainApi.apiGetQiniuToken()
            .then((res) => {
              let { data: qiNiuToken } = res
              const qiNiu = require('qiniu-js')
              const observable = qiNiu.upload(file, qiNiuKey, qiNiuToken)
              const subscription = observable.subscribe(() => {
              },() => {
              },() => {
                this.uploadImgSrc = `http://qiniu.yangpihu.net/${qiNiuKey}`
                return resolve()
              })
            });
      })
    },
    reqSave() {
      this.$mainApi.apiNewPostFeedback(this.message, this.uploadImgSrc, 3)
          .then((res) => {
            uni.showModal({
              title: "提示",
              content: "提交成功",
              showCancel: false,
              success: () => {
                this.uploadImgSrc = ""
                this.message = ""
                this.uploadPreview = ""
              }
            })
            uni.hideLoading();
          });
    },
    submit() {
      if (this.message.length < 10) {
        uni.showModal({
          title: "提示",
          content: "请至少输入10个文字",
        })
      } else {
        if (!!this.uploadImg) {
          uni.showLoading({
            title: "正在提交"
          });
          this.uploadQiNiuFile(this.uploadImgObj)
              .then(() => {
                this.reqSave()
              })
        } else {
          uni.showLoading({
            title: "正在提交"
          })
          this.reqSave()
        }
      }
    }
  }
})
</script>
<style lang="scss" scoped>
.page-container {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: #efefef;
  display: flex;
  flex-direction: column;
}

.header-container {
  color: #fff;
  height: 24px;
  padding: 15px;
  flex-shrink: 0;
  display: flex;
  background: #006b65;
}

.header-container .left,
.header-container .right {
  font-size: 16px;
  width: 100px;
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.header-container .center {
  flex-grow: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

.main-container {
  flex-grow: 1;
  overflow: auto;
}

.form-container {
  padding: 7px;
  display: flex;
  flex-direction: column;

  .label {
    color: #333;
    font-size: 16px;
  }

  .item {
    textarea {
      width: 100%;
      height: 400px;
      border-color: transparent;
      border-radius: 15px;
      background: #ffffff;
    }

    .ctrls {
      display: flex;
      justify-content: space-between;
      margin-top: 20px;

      .camera {
        width: 50px;
        height: 50px;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .tip {
        color: #666666;
        font-size: 13px;
        line-height: 50px;
      }
    }
  }
}

.text-body {
  //padding: 8px;
  margin-top: 10px;
}

.title-18 {
  font-size: 18px;
  font-weight: 500;
  text-align: center;
}

.btn-res {
  margin-top: 20px;
  background: #007aff;
  color: #fff;
}
</style>
